﻿using AuthCenter.Api.Read.Application.Command.Role;
using AuthCenter.Api.Read.Application.Output;
using AuthCenter.Application;
using AuthCenter.Domain;
using AuthCenter.ErrorCode;
using AutoMapper;
using MediatR;
using System.Collections.Generic;

namespace AuthCenter.Api.Read.Application.Hander.Role
{
    public class GetRoleShowHander : IRequestHandler<GetRoleShowCommand, ApiResult<List<RoleOutput>>>
    {
        private readonly IBaseRepository<RoleModel> _roleRepository;
        private readonly IBaseRepository<UserRoleModel> _userRoleRepository;
        private readonly IBaseRepository<UserModel> _userRepository;
        private readonly IMapper _mapper;
        public GetRoleShowHander(IBaseRepository<RoleModel> roleRepository, IMapper mapper, IBaseRepository<UserRoleModel> userRoleRepository, IBaseRepository<UserModel> userRepository)
        {
            _roleRepository = roleRepository;
            _mapper = mapper;
            _userRoleRepository = userRoleRepository;
            _userRepository = userRepository;
        }
        /// <summary>
        /// 显示角色
        /// </summary>
        /// <returns></returns>
        public Task<ApiResult<List<RoleOutput>>> Handle(GetRoleShowCommand request, CancellationToken cancellationToken)
        {
            ApiResult<List<RoleOutput>> res = new ApiResult<List<RoleOutput>> ();
            var models = _roleRepository.GetAll().OrderByDescending(x => x.Id).Where(x => x.IsDeleted == false);
            var list = _mapper.Map<List<RoleOutput>>(models);
            res.Code = ApiEnums.OK;
            res.Message = "显示成功";
            res.Data = list.ToList();
            return Task.FromResult(res);
        }
    }
}
